import angular from 'angular'
import 'apiService'
import _ from 'lodash'
import ui from '@yunke/core/ui';

const app = angular.module('ProcessCenter');

app.controller('deliveryOrderController', ['$rootScope', '$scope', 'api', function ($rootScope, $scope, api) {

    const service = api.getService();

    const {pageIndex, pageSize} = api;

    $scope.Page = {
        query: {},
        filter: {},
        rowNames: 'rows',
        pageIndexName: 'page',
        pageSizeName: 'page-size',
        pagination: {
            params: {},
            total: 0,
            change: function (pageIndex, pageSize) {
                const thiz = $scope.Page;
                thiz.pagination.params[thiz['pageIndexName']] = pageIndex;
                thiz.pagination.params[thiz['pageSizeName']] = pageSize;
                thiz.list();
            }
        },
        impl: {
            filterSearchResult: function (res) {
                const thiz = $scope.Page;
                thiz.setItems(res.data.data.data);
                thiz.pagination.total = res.data.data.total;
            }
        },
        getItems: function () {
            return this[this.rowNames];
        },
        setItems: function (items) {
            this[this.rowNames] = items;
            return this.getItems();
        },
        list: function (pageIndex) {
            const thiz = this;
            thiz.pagination.params[thiz['pageIndexName']] = pageIndex || thiz.pagination.params[thiz['pageIndexName']];
            return service.getDeliveryOrders(thiz.getQuery(pageIndex))
                .then(function (res) {
                    return thiz.impl.filterSearchResult(res);
                })
        },
        getQuery: function (pageIndex) {
            return angular.extend({}, this.query, this.filter, this.pagination.params,
                {[this['pageIndexName']]: pageIndex === undefined ? this.pagination.params[this['pageIndexName']] : pageIndex});
        },
        reset: function () {
            return this.list(pageIndex);
        },
        init: function () {
            this[this.rowNames] = this[this.rowNames] || [];
            this.pagination.params[this['pageIndexName']] = pageIndex;
            this.pagination.params[this['pageSizeName']] = pageSize;
            return this.reset();
        }
    };

    $scope.dhTpl = require('../dialog/ch.html');

    $scope.beforeDhShow = ($dialogScope, name) => {
        $dialogScope.name = name;
    };

    $scope.orderQr = (guid) => {

        return service.orderQr({
            delivery_guid: guid
        })
            .then(() => $scope.Page.list($scope.Page.pagination.params.pageIndex))
            .then(() => {
                ui.showSucTip('确认成功');
            })

    };

    $scope.types = [
        {
            text: '全部',
            value: undefined
        },
        {
            text: '正常',
            value: '1'
        },
        {
            text: '特批',
            value: '4'
        }
    ];

    $scope.steps = [
        {
            text: '全部',
            value: undefined
        },
        {
            text: '未确认',
            value: '20'
        },
        {
            text: '已确认',
            value: '30'
        },
        {
            text: '已打回',
            value: '10'
        }
    ];

    $scope.search = _.debounce(() => {
        $scope.Page.list(1);
    }, 200);

    $scope.$watch('Page.filter', (newValue, oldValue) => {

        if (!_.eq(newValue, oldValue)) {
            $scope.search();
        }

    }, true);

    $scope.orderDh = ($data, guid) => {

        return service.orderDh({
            delivery_guid: guid,
            remarks: $data.remarks
        })
            .then(() => $scope.Page.list($scope.Page.pagination.params.pageIndex))
            .then(() => {
                ui.showSucTip('打回成功');
            })

    };

    $rootScope.$on('refreshDeliveryOrder', () => {
        $scope.Page.init();
    });

    $scope.Page.init();


}]);
export default app;
